#include "GUIManager.h"
#include "../Games/MyGame.h"

GUIManager * GUIManager::_instance = NULL;

GUIManager::GUIManager() {
	for(int i = 0; i < 10; i++) {
		menuOptions[i] = NULL;
		menuInfo[i] = NULL;
	}

	backgroundpos.x = 6.5f;
	backgroundpos.y = 4.5f;
	backgroundpos.z = -12.0;

	numberOfOptions = 0;
	optionSelected = 0;
	optionsColor.r = optionsColor.g = optionsColor.b = 1;
	//glutKeyboardFunc(keyboard);

}

GUIManager::~GUIManager() {

}

void GUIManager::create()
{
	if (_instance == NULL)
	{
		_instance = new GUIManager();
	}
}

void GUIManager::destroy()
{
	delete _instance;
	_instance = NULL;
}

GUIManager* GUIManager::Instance () {
	return _instance;
}

void GUIManager::onKeyPressed(int key) {
	if(key == VK_DOWN) {
		if(optionSelected < numberOfOptions-1)
			optionSelected++;
	}
	else if(key == VK_UP) {
		if(optionSelected >= 1)
			optionSelected--;
	}
	else if(key == VK_RETURN) {
		MyGame::Instance()->callMethod(menuOptions[optionSelected]);
	}
	else if(key == 'K') {
		MyGame::Instance()->callMethod("Save Game");
	}
	else if(key == 'L') {
		MyGame::Instance()->callMethod("Load Game");
	}
}

void GUIManager::onKeyReleased(int key) {}

void GUIManager::addMenuBckgnd(char *menuBckgndTexture) {
	strcpy(this->menuBackgroundTexture,menuBckgndTexture);
}

bool GUIManager::addMenuOption(char *menuOption) {
	for(int i = 0; i < 10; i++) {
		if(menuOptions[i] == NULL) {
			menuOptions[i] = (char*) malloc(strlen(menuOption));
			strcpy(menuOptions[i],menuOption);
			this->numberOfOptions++;
			return true;
		}
	}
	return false;
}

bool GUIManager::addMenuInfo(char *menuInformation) {

	for(int i = 0; i < 10; i++) {
		if(menuInfo[i] == NULL) {
			menuInfo[i] = (char*) malloc(strlen(menuInformation));
			strcpy(menuInfo[i],menuInformation);
			this->numberOfInfos++;
			return true;
		}
	}
	return false;
}

void GUIManager::displayGameMenu() {
	float counter = 0;
	float counter2 = 0;
	ORIENTATION orientation;
	orientation.degree = 180;
	orientation.orientVect.x = 0;
	orientation.orientVect.z = 1;
	orientation.orientVect.y = 0;
	SCALE scale = {1.3,0.9,1};
	COLOR defaultcolor = {1,1,1};
	GameEngine::Instance()->getRenderer()->drawPieceQuad(menuBackgroundTexture,1,1,1,backgroundpos,orientation,scale,1000,600,defaultcolor);
	for(int i = 0; i < 10; i++) {
		if(this->menuOptions[i] != NULL) {
			if(this->optionSelected == i) {
				if(optionsColor.r == optionsColor.g == optionsColor.b == 1) {
					optionsColor.g = optionsColor.b = 0;
				}
				else optionsColor.r = 1;
			}
			else optionsColor.g = optionsColor.b = 1;
			GameEngine::Instance()->getRenderer()->writeOnScreen(this->menuOptions[i],-0.5,1-counter/1.5,18,optionsColor);
			counter++;
		}
		if(this->menuInfo[i] != NULL) {
			GameEngine::Instance()->getRenderer()->writeOnScreen(this->menuInfo[i], -5.4,-4+counter2,14,defaultcolor);
			counter2 += 0.3f;
		}
	}
}
